package main

import (
	"encoding/json"
	"fmt"
)

//匿名字段
//字段比较少
type j1 struct {
	string
	int
}

//结构体嵌套
type address struct {
	provide string
	city    string
}

func (a address) f1() {
	fmt.Printf("%s有活动\n", a.provide)
}

type person struct {
	name    string
	address //匿名嵌套  或者addr address
}

func (p person) f2() {
	fmt.Printf("%s去旅游\n", p.name)
}

//结构体和json   这里字段首字母必须大写 要不然拿不到值
type jg1 struct {
	Name string `json:"name" db:"name" ini:"name"`
	Age  int
}

func main() {
	var j2 = j1{
		"周茹发",
		12,
	}
	fmt.Println(j2.string)
	// 结构体嵌套
	j3 := person{
		name: "受打击了",
		address: address{
			provide: "北京",
			city:    "东城区",
		},
	}
	fmt.Println(j3.name)
	fmt.Println(j3.address.city)
	fmt.Println(j3.city) //匿名嵌套有多个就必须写全了
	//模拟继承
	p1 := person{
		name: "攻击力",
		address: address{
			provide: "北京",
			city:    "东城区",
		},
	}
	p1.f2()
	p1.f1()
	//结构体和json
	p3 := jg1{
		Name: "攻击力",
		Age:  12,
	}
	p2, err := json.Marshal(p3)
	if err != nil {
		fmt.Printf("错误信息是：%v", err)
	}
	fmt.Println(string(p2))
	fmt.Printf("%#v\n", string(p2))
	//反序列化
	str := `{"name":"发深V的"，"age":12}`
	var p4 jg1
	json.Unmarshal([]byte(str), &p4)
	fmt.Printf("反序列化结果是：%#v\n", p4)
}
